package com.marry.intelli.plugin;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.file.PsiDirectoryFactory;
import com.intellij.psi.impl.source.xml.XmlFileImpl;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * @author boyfighting@qq.com
 * @desc
 * @date 2019/4/25
 */
public class HelloWorldAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {

        Project project = e.getData(PlatformDataKeys.PROJECT);
        PsiDirectory baseDir = PsiDirectoryFactory.getInstance(project).createDirectory(project.getBaseDir());
        System.out.println(baseDir.getParent());
        List<PsiFile> psiFileList = new ArrayList<>();
        showAndCollectFiles(baseDir.getParent(), psiFileList);
        Map<String, XmlTag> sqlMap = new HashMap<String, XmlTag>();
        for (PsiFile psiFile : psiFileList) {
            if (psiFile instanceof XmlFile) {
                XmlFile xmlFile = (XmlFileImpl) psiFile;
                XmlTag rootTag = xmlFile.getRootTag();
                if (!"sqlMap".equals(rootTag.getName())) {
                    continue;
                }
                String namespace = rootTag.getAttributeValue("namespace");
                for (XmlTag child : rootTag.getSubTags()) {
                    if (!"sql".equals(child.getName())) {
                        continue;

                    }
                    String sqlId = child.getAttributeValue("id");

                    String mapKey = namespace + "." + sqlId;
                    sqlMap.put(mapKey, child);
                }

                rootTag.getSubTags();
            }


        }
        System.out.println(psiFileList);
        System.out.println(sqlMap);

        SearchDiaog searchDiaog = new SearchDiaog();
        searchDiaog.setTitle("查询SQL");
        searchDiaog.setContextText("");
        searchDiaog.setProject(project);
        searchDiaog.setSqlMap(sqlMap);
        searchDiaog.setBounds(new Rectangle(600, 600));

        searchDiaog.setLocationRelativeTo(WindowManager.getInstance().getFrame(project));
        searchDiaog.setVisible(true);

    }

    private void showAndCollectFiles(PsiDirectory baseDir, List<PsiFile> psiFileList) {
        PsiDirectory[] resources = baseDir.getSubdirectories();
        if (resources.length == 0) {
            PsiFile[] psiFiles = baseDir.getFiles();
            for (PsiFile psiFile : psiFiles) {
                if (psiFile.getName().startsWith("sql")) {
                    psiFileList.add(psiFile);
                }

            }

        }
        for (PsiDirectory directory : resources) {
            if (!directory.isDirectory()) {
                PsiFile[] psiFiles = baseDir.getFiles();
                for (PsiFile psiFile : psiFiles) {
                    if (psiFile.getName().startsWith("sql")) {
                        psiFileList.add(psiFile);
                    }

                }


            } else {
                showAndCollectFiles(directory, psiFileList);
            }
        }

    }
}
